/*
 * FNISVGEditor - JavaScript - based editor for SVG graphics 
 * Copyright (C) 2008 - 2009 Antti Leppä / Foyt
 * http://www.foyt.fi
 * 
 * License: 
 * 
 * Licensed under GNU Lesser General Public License Version 2.1 or later (the "LGPL") 
 * http://www.gnu.org/licenses/lgpl.html
 */
FNISVGPluginLoader = {
  isLoaded: function (pluginName) {
    return this._pluginURLs.get(pluginName) != undefined;
  },
  load: function (pluginName, pluginURL, loadMode, afterLoad, afterFail) {
    if (this.isLoaded(pluginName)) {
      afterLoad(pluginName, this.getPluginClass(pluginName));
    } else {
      if (loadMode == 'script') {
        this._scriptLoad(pluginName, pluginURL, afterLoad, afterFail);
      } else {
        this._ajaxLoad(pluginName, pluginURL, afterLoad, afterFail);
      }
    }
  },
  _ajaxLoad: function (pluginName, pluginURL, afterLoad, afterFail) {
    var _this = this;
    new Ajax.Request(pluginURL + '/plugin.js', {
      onSuccess: function(transport) {         
        try {
          _this._pluginURLs.set(pluginName, pluginURL);
          
          for (var i = 0; i < SVGLocale.getLanguages().length; i++) {
            var lang = SVGLocale.getLanguages()[i];
            SVGLocale.loadLocale(lang, pluginURL + "/lang/" + lang + ".json");
          }
          
          var loadFunc = null;
          loadFunc = function (event) {
            Event.stopObserving(document, "svgEditorPluginLoaded:" + pluginName, loadFunc);
            var pluginClass = event.memo.pluginClass;
            FNISVGPluginLoader.setPluginClass(pluginName, pluginClass);
            afterLoad(pluginName, pluginClass);                  
          };
          
          Event.observe(document, "svgEditorPluginLoaded:" + pluginName, loadFunc);
        } catch (e) {
          alert("Error occured while loading plugin " + pluginName + ": " + e);
        }
      },
      onFailure: function(transport){
        afterFail();
      },
      asynchronous: false
    });
  },
  _scriptLoad: function (pluginName, pluginURL, afterLoad, afterFail) {
    this._pluginURLs.set(pluginName, pluginURL);

    for (var i = 0; i < SVGLocale.getLanguages().length; i++) {
      var lang = SVGLocale.getLanguages()[i];
      SVGLocale.loadLocale(lang, pluginURL + "/lang/" + lang + ".json");
    }
    
    var loadFunc = null;
    loadFunc = function (event) {
      Event.stopObserving(document, "svgEditorPluginLoaded:" + pluginName, loadFunc);
      var pluginClass = event.memo.pluginClass;
      FNISVGPluginLoader.setPluginClass(pluginName, pluginClass);
      afterLoad(pluginName, pluginClass);                  
    };
    
    Event.observe(document, "svgEditorPluginLoaded:" + pluginName, loadFunc);
    
    var scriptElement = new Element("script", {
      type: "text/javascript",
      src: pluginURL + '/plugin.js'
    });
    
    document.body.appendChild(scriptElement);
  },
  getURL: function (pluginName) {
    return this._pluginURLs.get(pluginName);
  },
  getPluginClass: function (pluginName) {
    return this._pluginClasses.get(pluginName);
  },
  setPluginClass: function (pluginName, pluginClass) {
    this._pluginClasses.set(pluginName, pluginClass);
  },
  _pluginClasses: new Hash(),
  _pluginURLs: new Hash()
};